package CI.aufgabe2.cip.mynodes;

import java.util.List;

import CI.aufgabe1.ProHHScanner;
import CI.aufgabe2.cip.SymbolTable;
import CI.aufgabe2.cip.base.AbstractDescr;
import CI.aufgabe2.cip.mynodes.MyAbstractNode;

public class MyBinNode extends MyAbstractNode {

	private static final long serialVersionUID = 1L;

	MyAbstractNode leftNode;
	MyAbstractNode rightNode;
	String operator;

	public String getOperator() {
		return operator;
	}

	public void setOperator(String operator) {
		this.operator = operator;
	}

	public MyBinNode() {
		setOperator("");
		leftNode = null;
		rightNode = null;
	};

	public MyBinNode(String fop) {
		setOperator(fop);
		leftNode = null;
		rightNode = null;
	};

	public MyBinNode(String fop, MyAbstractNode fl, MyAbstractNode fr) {
		setOperator(fop);
		leftNode = fl;
		rightNode = fr;
	};

	public void setLeftNode(MyAbstractNode fl) {
		leftNode = fl;
	};

	public void setRightNode(MyAbstractNode fr) {
		rightNode = fr;
	};

	public MyAbstractNode getLeftNode() {
		return leftNode;
	};

	public MyAbstractNode getRightNode() {
		return rightNode;
	};

	public void print() {
		trace("MyBinNode " + getOperator());
		if (leftNode != null)
			leftNode.print();
		else
			outStr(" null ");
		if (rightNode != null)
			rightNode.print();
		else
			outStr(" null ");
		unindent();
	};
	
	
	public AbstractDescr compile(SymbolTable sym) {
		leftNode.compile(sym);
		List<MyIdNode> varList = sym.getVarFormalParameters();
		if(leftNode instanceof MyIdNode || leftNode instanceof MyRecordSelectorNode || leftNode instanceof MyArraySelectorNode){
			write("CONT, 1");
			if(varList.contains(leftNode))
				write("CONT, 1");
		}
			
		rightNode.compile(sym);
		if(rightNode instanceof MyIdNode || rightNode instanceof MyRecordSelectorNode || rightNode instanceof MyArraySelectorNode){
			write("CONT, 1");
			if(varList.contains(rightNode))
				write("CONT, 1");
		}
			
		
		if (operator.equals("*")){
		write("MUL");
		}
		else if (operator.equals("/")){
		write("DIV");
		}
		else if (operator.equals("-")){
		write("SUB");
		}
		else if (operator.equals("+")){
		write("ADD");
		}
		else if (operator.equals("=")){
		write("EQ");
		}
		else if (operator.equals(">")){
		write("GT");
		}
		else if (operator.equals(">=")){
		write("GE");
		}
		else if (operator.equals("<")){
		write("LT");
		}
		else if (operator.equals("<=")){
		write("LE");
		}
		return null;
	}
}